package com.zqx.spmab.controller.commons;

import com.zqx.spmab.commons.JsonResultBean;
import com.zqx.spmab.pojo.form.AttachmentForm;
import com.zqx.spmab.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

/**
 * 功能描述：上传文件公共类
 * Created by Zqx on 2018/4/24.
 */
@RequestMapping("/file")
@Controller
public class FileUploadController {

    @Autowired
    private AttachmentService attachmentService;


    /**
     * 文件上传-
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultBean uploadFile(@RequestParam("file") MultipartFile file,final AttachmentForm form,final HttpServletRequest request,
                                      final HttpServletResponse response) throws Exception {

        String id = attachmentService.uploadFile(file,form);
        return new JsonResultBean(id);
    }

    /**
     * 文件下载
     */
    @RequestMapping(value = "/download")
    public void download(final String id, final HttpServletResponse response) throws FileNotFoundException {
        attachmentService.download(id, response);
    }
}
